package org.nanhill.xmlro.gsp.model;

import java.lang.reflect.Field;
import java.util.List;

import org.nanhill.commons.lang.ReflectKit;
import org.nanhill.xmlro.Serializer;
import org.nanhill.xmlro.XmlroException;
import org.nanhill.xmlro.XmlroOutput;
import org.nanhill.xmlro.gsp.GspOutput;

/**
 * Serializing an object for known object types.
 */
/**
 * @author alin
 * 
 */
class GspJavaSerializer implements Serializer {
	public void writeObject(Object obj, XmlroOutput out) throws XmlroException {
		GspOutput gout = (GspOutput) out;
		gout.writeMapBegin();
		List<Field> fields = ReflectKit.getAllAccessibleField(obj);
		// gout.writeMapMemberBegin(field.getName());
		// try {
		// gout.writeObject(field.get(obj));
		// } catch (IllegalArgumentException e) {
		// throw new XmlroException(e);
		// } catch (IllegalAccessException e) {
		// throw new XmlroException(e);
		// }
		// gout.writeMapMemberEnd();
		// }
		// }
		for (Field field : fields) {
			String key = field.getName();
			gout.writeMapMemberBegin(key);
			try {
				gout.writeObject(field.get(obj));
			} catch (IllegalArgumentException e) {
				throw new XmlroException(e);
			} catch (IllegalAccessException e) {
				throw new XmlroException(e);
			}
			gout.writeMapMemberEnd(key);
		}
		gout.writeMapEnd();
	}
}
